﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Client.Resources;
using Client.ViewModels;
using Client.ViewModels.WebImport;
using Contracts.Entities;

namespace Client.Views.WebImport
{
    /// <summary>
    /// Interaktionslogik für WizardView.xaml
    /// </summary>
    public partial class WebImportWizardView : Window
    {
        public WebImportWizardView()
        {
            InitializeComponent();
        }

        private void Cancel(object sender, EventArgs e)
        {
            DialogResult = false;
        }

        private void WizardCompleted()
        {
            DialogResult = true;
        }

        public WizardViewModel ViewModel 
        {
            get 
            {
                return (DataContext as WizardViewModel); 
            }
            set
            {
                DataContext = value;
            }
        }

        public bool? ShowDialog(IEnumerable<MovieItem> items)
        {
            var pages = new List<WizardPageViewModel>();
            var searchItems = items.Distinct(new MovieNameComparer());

            pages.Add(new SearchViewModel(searchItems));
            pages.Add(new SearchResultsViewModel(searchItems));
            pages.Add(new SearchImportViewModel(items));

            ViewModel = new WizardViewModel(pages);
            ViewModel.OnCancel += (m, n) => Cancel(m, n);
            ViewModel.OnCompleted += WizardCompleted;

            return base.ShowDialog();
        }
    }
}
